package com.haobao.telephonerecord.utils;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @author ypk
 * 创建日期：2021/12/20  14:46
 * 描述：
 */
public class FileUtils {


    /**
     * 删除 .nomedia 文件夹
     *
     * @param dir
     */
    public static void deleteFiles(File dir) {
        if (dir.exists()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFiles(file);
                } else if (file.isFile() && file.getName().equals(".nomedia")) {
                    file.delete();
                }
            }
            dir.delete();
        }
    }


    //https://blog.csdn.net/qq_33266474/article/details/82878754
    public static void deleteNoMediaFiles() {
        String pathXiaom = Environment.getExternalStorageDirectory().toString() + "/MIUI/sound_recorder/";
        delete_nomedia_files(new File(pathXiaom));

        String pathHUAWEI1 = Environment.getExternalStorageDirectory().toString() + "/record";
        String pathHUAWEI2 = Environment.getExternalStorageDirectory().toString() + "/Sounds/CallRecord";
        delete_nomedia_files(new File(pathHUAWEI1));
        delete_nomedia_files(new File(pathHUAWEI2));
    }

    public static void delete_nomedia_files(File dir) {
        if (dir.exists()) {
            File[] files = dir.listFiles();
            for (File file : files) {
                System.out.println(".delete_nomedia_files111");
                if (file.isDirectory()) {
                    delete_nomedia_files(file);
                } else if (file.isFile() && file.getName().equals(".nomedia")) {
                    file.delete();
                }
            }
            dir.delete();
        }
    }


    /**
     * 获取指定目录内所有文件路径
     *
     * @param dirPath  需要查询的文件目录
     * @param fileType 查询类型，比如mp3什么的
     */
    public static Vector<File> getAllFiles(String dirPath, String fileType) {
        Vector<File> fileVector = new Vector<>();
        File f = new File(dirPath);
        if (!f.exists()) {//判断路径是否存在
            return fileVector;
        }
        File[] files = f.listFiles();
        if (files == null) {//判断权限
            return fileVector;
        }
        Vector<File> vecFile = new Vector<File>();
        for (File _file : files) {//遍历目录
            if (_file.isFile() && _file.getName().endsWith(fileType)) {
                vecFile.add(_file);
            } else if (_file.isDirectory()) {//查询子目录
                getAllFiles(_file.getAbsolutePath(), fileType);
            } else {
            }
        }
        return vecFile;
    }

    public static Vector<File> getAllFiles(String dirPath) {
        Vector<File> fileVector = new Vector<>();
        File f = new File(dirPath);
        if (!f.exists()) {//判断路径是否存在
            return fileVector;
        }
        File[] files = f.listFiles();
        if (files == null) {//判断权限
            return fileVector;
        }
        Vector<File> vecFile = new Vector<File>();
        for (File _file : files) {//遍历目录
            if (_file.isFile()) {
                vecFile.add(_file);
            } else if (_file.isDirectory()) {//查询子目录
                getAllFiles(_file.getAbsolutePath());
            } else {
            }
        }
        return vecFile;
    }


    /**
     * 获取dataFile文件夹下的所有文件
     */
    public static ArrayList<String> getAllDataFileName(String collectionPath) {
        // 文件夹路径
        // String collectionPath = makeDataFile().getPath();

        ArrayList<String> fileList = new ArrayList<>();

        File file = new File(collectionPath);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                System.out.println("文     件：" + tempList[i].getName());
                // tempList[i].toString();// 路径
                // tempList[i].getName();// 文件名
                // 文件名
                String fileName = tempList[i].getName();
                if (fileName.endsWith("shp")) {
                    // 文件大小
                    // String fileSize = FileSizeUtil.getAutoFileOrFilesSize(tempList[i].toString());
                    fileList.add(fileName);
                }

            }
        }

        return fileList;
    }
}
